package com.suixun.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Service;

import com.suixun.dao.SysOrgnizationMapper;
import com.suixun.entity.SysOrgnization;
import com.suixun.entity.SysOrgnizationExample;
import com.suixun.entity.ext.ExtSysOrgnization;
import com.suixun.service.RegionService;

import rop.utils.spring.CollectionUtils;

@Service
public class RegionServiceImpl implements RegionService {

	@Resource
	private SysOrgnizationMapper sysOrgnizationMapper;
	@Resource
	private SqlSessionTemplate sqlSessionTemplate;

	@Override
	public List<SysOrgnization> getRegions(String uuid) {
		SysOrgnizationExample sysOrgnizationExample = new SysOrgnizationExample();
		if (StringUtils.isBlank(uuid)) {
			// 查询国家
			sysOrgnizationExample.createCriteria().andOrgTypeEqualTo("国家");
		} else {
			sysOrgnizationExample.createCriteria().andPuuidEqualTo(uuid);
		}
		List<SysOrgnization> sysOrgnizationList = sysOrgnizationMapper.selectByExample(sysOrgnizationExample);
		return sysOrgnizationList;
	}

	@Override
	public SysOrgnization getRegion(String uuid) {
		return sysOrgnizationMapper.selectByPrimaryKey(uuid);
	}

	@Override
	public SysOrgnization getPRegion(String uuid) {
		SysOrgnization sysOrgnization = getRegion(uuid);
		if(sysOrgnization == null) {
			return null;
		}else {
			SysOrgnization parentOrgnization = getRegion(sysOrgnization.getPuuid());
			ExtSysOrgnization returnOrgnization = new ExtSysOrgnization();
			returnOrgnization.setChildOrgName(sysOrgnization.getOrgName());
			returnOrgnization.setOrgName(parentOrgnization.getOrgName());
			returnOrgnization.setOrgType(parentOrgnization.getOrgType());
			returnOrgnization.setPuuid(parentOrgnization.getPuuid());
			returnOrgnization.setUuid(parentOrgnization.getUuid());
			return returnOrgnization;
		}
	}
	
	private SysOrgnization getPRegionOther(String uuid) {
		SysOrgnization sysOrgnization = getRegion(uuid);
		if(sysOrgnization == null) {
			return null;
		}else {
			if(StringUtils.isBlank(sysOrgnization.getPuuid())) {
				return null;
			}
			return getRegion(sysOrgnization.getPuuid());
		}
	}

	@Override
	public List<SysOrgnization> getChildRegions(String uuid) {
		String uuids = getChildRegionIds(uuid);
		if(StringUtils.isNotBlank(uuids)) {
			List<String> uuidList = new ArrayList<>();
			String[] uuidArray = uuids.split(",");
			for(String orgId : uuidArray) {
				uuidList.add(orgId);
			}
			SysOrgnizationExample sysOrgnizationExample = new SysOrgnizationExample();
			sysOrgnizationExample.createCriteria().andUuidIn(uuidList);
			return sysOrgnizationMapper.selectByExample(sysOrgnizationExample);
		}else {
			return null;
		}
	}

	@Override
	public String getChildRegionIds(String uuid) {
		return sqlSessionTemplate.selectOne("RegionExtendSql.getChildrenRegionIds", uuid);
	}

	@Override
	public String getUuidByOrgName(String province, String city, String town) {
		Map<String, Object> map = new HashMap<>();
		map.put("province", province);
		map.put("city", city);
		map.put("town", town);
		return sqlSessionTemplate.selectOne("RegionExtendSql.getUuidByOrgName", map);
	}

	@Override
	public List<SysOrgnization> getAllPRegionsByUuid(String uuid) {
		if(StringUtils.isBlank(uuid)) {
			return null;
		}
		List<SysOrgnization> regionList = new ArrayList<>();
		return getAllPRegionsByUuid(uuid, regionList);
	}
	
	private List<SysOrgnization> getAllPRegionsByUuid(String uuid, List<SysOrgnization> regionList) {
		if(StringUtils.isBlank(uuid)) {
			return regionList;
		}else {
			SysOrgnization orgnization = getPRegionOther(uuid);
			if(orgnization == null) {
				return regionList;
			}else {
				regionList.add(orgnization);
				return getAllPRegionsByUuid(orgnization.getUuid(), regionList);
			}
		}
	}

	@Override
	public List<String> getPUuidsByOrgName(String province, String city, String town) {
		// 获取该地区对应的uuid
		String uuid = getUuidByOrgName(province, city, town);
		// 通过uuid获取所有的父级机构
		List<SysOrgnization> orgnizationList = getAllPRegionsByUuid(uuid);
		if(CollectionUtils.isEmpty(orgnizationList)) {
			return null;
		}else {
			List<String> pUuidList = new ArrayList<>();
			pUuidList.add(uuid);
			for(SysOrgnization orgnization : orgnizationList) {
				pUuidList.add(orgnization.getUuid());
			}
			return pUuidList;
		}
	}
	
}
